package com.micai.springboot.util;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableName;
import com.micai.springboot.base.Parent;
import com.micai.springboot.entity.UserEntity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaoxinguo on 2018/1/30.
 */
public class ReflectionUtils extends Parent<UserEntity> {

    public static String getTableName(Object object) {
        TableName tableName = object.getClass().getAnnotation(TableName.class);
        return tableName.value();
    }

    public static List<Object> getFields(Object object) {
        List<Object> list = new ArrayList<Object>();
        Field[] fields = object.getClass().getDeclaredFields();
        for(Field field : fields) {
            TableField tableField = field.getAnnotation(TableField.class);
            list.add(tableField.value());
        }
        return list;
    }

    public static void main(String[] args) {
        // 获取类上的注解值
        /*TableName annotation = UserEntity.class.getAnnotation(TableName.class);
        System.out.println(annotation.value());*/

        String tableName = ReflectionUtils.getTableName(new UserEntity());
        System.out.println(tableName);

        List<Object> fields = ReflectionUtils.getFields(new UserEntity());
        for (Object o : fields) {
            System.out.println(o);
        }

        // 获取所有变量,并获取指定方法上的注解信息
        /*Field[] fields = UserEntity.class.getDeclaredFields();
//        Field field = null;
        for(Field field : fields){
            *//*if(f.getName().equals("userName")){
                field = f;
                break;
            }*//*
            TableField tableField = field.getAnnotation(TableField.class);
            System.out.println(tableField.value());
        }*/

    }

}
